package com.itheima.sfbx.file.adapter.impl;

import com.itheima.sfbx.file.adapter.FileStorageAdapter;
import com.itheima.sfbx.file.handler.FileStorageHandler;
import com.itheima.sfbx.file.handler.aliyun.properties.OssAliyunConfigProperties;
import com.itheima.sfbx.framework.commons.constant.file.FileConstant;
import com.itheima.sfbx.framework.commons.dto.file.FileVO;
import com.itheima.sfbx.framework.commons.dto.file.FilePartVO;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.framework.commons.utils.RegisterBeanHandler;
import com.itheima.sfbx.file.pojo.File;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName FileStorageAdapterImpl.java
 * @Description 文件存储适配处理
 */
@Slf4j
@Component
public class FileStorageAdapterImpl implements FileStorageAdapter {
    @Autowired
    RegisterBeanHandler registerBeanHandler;

    @Autowired
    private OssAliyunConfigProperties ossAliyunConfigProperties;

    private static final Map<String,String> fileStorageHandlers = new HashMap<>();

    static {
        fileStorageHandlers.put(FileConstant.ALIYUN_OSS,"ossFileStorageHandler");
        fileStorageHandlers.put(FileConstant.QINIU_KODO,"kodoFileStorageHandler");
    }

    /**
     * 统一处理 Bucket 名称的私有辅助方法。
     * 强制使用服务端配置的默认值，忽略任何上游传入的值。
     * @param ignoredIncomingBucketName 忽略这个参数
     * @return 最终决定使用的 bucketName
     */
    private String resolveBucketName(String ignoredIncomingBucketName) {
        // 打印日志以便调试，可以看到我们忽略了传入的值
        System.out.println("--- 存储桶名称强制覆盖 --- Incoming (ignored): " + ignoredIncomingBucketName + " ---");
        // 无论上游传来什么，都强制返回 Nacos 中配置的 Bucket 名称
        String resolvedName = ossAliyunConfigProperties.getBucketName();
        System.out.println("--- 存储桶名称强制覆盖 --- Returning from config: " + resolvedName + " ---");
        return resolvedName;
    }

    @Override
    public String uploadFile(FileVO fileVO, InputStream inputStream) {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(fileVO.getStoreFlag())?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(fileVO.getStoreFlag());
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 调用辅助方法，强制覆盖 bucketName
        String bucketName = resolveBucketName(fileVO.getBucketName());

        return fileStorageHandler.uploadFile(fileVO.getSuffix(), fileVO.getFileName(), bucketName, fileVO.getAutoCatalog(), inputStream);
    }

    @Override
    public File initiateMultipartUpload(FileVO fileVO) {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(fileVO.getStoreFlag())?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(fileVO.getStoreFlag());
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 调用辅助方法，强制覆盖 bucketName
        String bucketName = resolveBucketName(fileVO.getBucketName());

        return fileStorageHandler.initiateMultipartUpload(fileVO.getSuffix(),fileVO.getFileName(), bucketName, fileVO.getAutoCatalog());
    }

    @Override
    public String uploadPart(FilePartVO filePartVo, InputStream inputStream) {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(filePartVo.getStoreFlag())?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(filePartVo.getStoreFlag());
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 调用辅助方法，强制覆盖 bucketName
        String bucketName = resolveBucketName(filePartVo.getBucketName());

        return fileStorageHandler.uploadPart(filePartVo.getUploadId(),filePartVo.getFileName(),filePartVo.getPartNumber(),filePartVo.getPartSize(), bucketName, inputStream);
    }

    @Override
    public String completeMultipartUpload(FileVO fileVO) {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(fileVO.getStoreFlag())?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(fileVO.getStoreFlag());
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 调用辅助方法，强制覆盖 bucketName
        String bucketName = resolveBucketName(fileVO.getBucketName());

        return fileStorageHandler.completeMultipartUpload(fileVO.getUploadId(),fileVO.getPartETags(),fileVO.getFileName(), bucketName);
    }

    @Override
    public InputStream downloadFile(String storeFlag,String bucketName,String pathUrl) throws IOException {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 强制覆盖 bucketName
        String finalBucketName = resolveBucketName(bucketName);

        return fileStorageHandler.downloadFile(finalBucketName, pathUrl);
    }

    @Override
    public void delete(String storeFlag,String bucketName,String pathUrl) {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 强制覆盖 bucketName
        String finalBucketName = resolveBucketName(bucketName);

        fileStorageHandler.delete(finalBucketName, pathUrl);
    }

    @Override
    public void deleteBatch(String storeFlag,String bucketName,List<String> pathUrls) {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 强制覆盖 bucketName
        String finalBucketName = resolveBucketName(bucketName);

        fileStorageHandler.deleteBatch(finalBucketName, pathUrls);
    }

    @Override
    public String getFileContent(String storeFlag,String bucketName,String pathUrl) throws IOException {
        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);

        // 强制覆盖 bucketName
        String finalBucketName = resolveBucketName(bucketName);

        return fileStorageHandler.getFileContent(finalBucketName, pathUrl);
    }











//原先的
//
//    @Autowired
//    RegisterBeanHandler registerBeanHandler;
//
//    // 1. 注入阿里云的配置类
//    @Autowired
//    private OssAliyunConfigProperties ossAliyunConfigProperties;
//
//    private static Map<String,String> fileStorageHandlers = new HashMap<>();
//
//    static {
//        fileStorageHandlers.put(FileConstant.ALIYUN_OSS,"ossFileStorageHandler");
//        fileStorageHandlers.put(FileConstant.QINIU_KODO,"kodoFileStorageHandler");
//    }
//
//    @Override
//    public String uploadFile(FileVO fileVO, InputStream inputStream) {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(fileVO.getStoreFlag())?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(fileVO.getStoreFlag());
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        return fileStorageHandler.uploadFile(fileVO.getSuffix(), fileVO.getFileName(), fileVO.getBucketName(), fileVO.getAutoCatalog(), inputStream);
//    }
//
//    @Override
//    public File initiateMultipartUpload(FileVO fileVO) {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(fileVO.getStoreFlag())?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(fileVO.getStoreFlag());
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        return fileStorageHandler.initiateMultipartUpload(fileVO.getSuffix(),fileVO.getFileName(),fileVO.getBucketName(),fileVO.getAutoCatalog());
//    }
//
//    @Override
//    public String uploadPart(FilePartVO filePartVo, InputStream inputStream) {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(filePartVo.getStoreFlag())?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(filePartVo.getStoreFlag());
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        return fileStorageHandler.uploadPart(filePartVo.getUploadId(),filePartVo.getFileName(),filePartVo.getPartNumber(),filePartVo.getPartSize(),filePartVo.getBucketName(),inputStream);
//    }
//
//    @Override
//    public String completeMultipartUpload(FileVO fileVO) {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(fileVO.getStoreFlag())?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(fileVO.getStoreFlag());
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        return fileStorageHandler.completeMultipartUpload(fileVO.getUploadId(),fileVO.getPartETags(),fileVO.getFileName(),fileVO.getBucketName());
//    }
//
//    @Override
//    public InputStream downloadFile(String storeFlag,String bucketName,String pathUrl) throws IOException {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        return fileStorageHandler.downloadFile(bucketName,pathUrl);
//    }
//
//    @Override
//    public void delete(String storeFlag,String bucketName,String pathUrl) {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        fileStorageHandler.delete(bucketName,pathUrl);
//    }
//
//    @Override
//    public void deleteBatch(String storeFlag,String bucketName,List<String> pathUrls) {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        fileStorageHandler.deleteBatch(bucketName,pathUrls);
//    }
//
//    @Override
//    public String getFileContent(String storeFlag,String bucketName,String pathUrl) throws IOException {
//        String fileStorageHandlerString = EmptyUtil.isNullOrEmpty(storeFlag)?
//                fileStorageHandlers.get(FileConstant.ALIYUN_OSS):fileStorageHandlers.get(storeFlag);
//        FileStorageHandler fileStorageHandler = registerBeanHandler.getBean(fileStorageHandlerString, FileStorageHandler.class);
//        return fileStorageHandler.getFileContent(bucketName,pathUrl);
//    }
}
